home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 February / EnigmA AMIGA RUN 04 (1996)(G.R. Edizioni)(IT)[!][issue 1996-02][Skylink CD III].iso / earcd / midi / midilb20.lha / examples / rsx.c < prev    next >
C/C++ Source or Header  |  1988-10-23  |  2KB  |  92 lines

  1. /* simplified receive system exclusive */
  2. /*    receives next sys/ex message and save it to a named file */
  3.  
  4. /*
  5.     shows usage of GetMidiPacket(), FreeMidiPacket() and msg
  6.     filtering with routes.
  7. */
  8.  
  9. #include <libraries/dos.h>
  10. #include <midi/midi.h>
  11. #include <functions.h>
  12. #include <fcntl.h>
  13.  
  14. void *MidiBase;
  15.  
  16. main(argc,argv)
  17. char **argv;
  18. {
  19.     extern int Enable_Abort;
  20.     int fo;                /* output file descriptor */
  21.     struct MidiPacket *packet=0, *getpacket();
  22.     struct MDest *dest=0;
  23.     struct MRoute *route=0;
  24.     static struct MRouteInfo routeinfo = { MMF_SYSEX };     /* receive only sys/ex msg's */
  25.     char *fname;
  26.  
  27.     printf ("Receive Sys/Ex\n");
  28.     Enable_Abort = 0;            /* disable auto CTRL-C handling */
  29.  
  30.     if (argc < 2) {
  31.     printf ("usage: rsx <file>\n");
  32.     exit (1);
  33.     }
  34.  
  35.     fname = argv[1];            /* get file name */
  36.  
  37.     if (!(MidiBase = OpenLibrary (MIDINAME,MIDIVERSION))) {
  38.     printf ("can't open midi.library\n");
  39.     goto clean;
  40.     }
  41.                     /* create our dest node (private) */
  42.     if (!(dest = CreateMDest (NULL,NULL))) {
  43.     printf ("can't create Dest\n");
  44.     goto clean;
  45.     }
  46.                     /* create our route to MidiIn */
  47.     if (!(route = MRouteDest ("MidiIn", dest, &routeinfo))) {
  48.     printf ("can't create Route (can't find MidiIn?)\n");
  49.     goto clean;
  50.     }
  51.                     /* create our output file (UNIX vanilla mode) */
  52.     if ( (fo=creat(fname,0666)) == -1) {
  53.     printf ("can't open %s\n",fname);
  54.     goto clean;
  55.     }
  56.                     /* wait for the next message, save it or skip if terminated */
  57.     if (packet = getpacket(dest)) {
  58.     if (write (fo,packet->MidiMsg,packet->Length) != packet->Length ||
  59.         close(fo) == -1) {
  60.         printf ("error writing file.\n");
  61.     }
  62.     printf ("sys/ex file \"%s\" saved. (%d bytes)\n",fname,packet->Length);
  63.     }
  64.     else {
  65.     printf ("save aborted.\n");
  66.     close(fo);
  67.     unlink(fname);
  68.     }
  69.  
  70. clean:
  71.     if (packet) FreeMidiPacket (packet);
  72.     if (route) DeleteMRoute (route);
  73.     if (dest) DeleteMDest (dest);
  74.     if (MidiBase) CloseLibrary (MidiBase);
  75. }
  76.  
  77.  
  78. /*
  79.    waits for next msg (will be sys/ex because that's all that our route
  80.    lets through).  Returns pointer to message.    If CTRL-C is hit before a
  81.    message arrives, returns NULL
  82. */
  83.  
  84. struct MidiPacket *getpacket (dest)
  85. struct MDest *dest;
  86. {
  87.     ULONG flags = SIGBREAKF_CTRL_C | (1L << dest->DestPort->mp_SigBit);
  88.  
  89.     if (Wait(flags) & SIGBREAKF_CTRL_C) return NULL;
  90.     return GetMidiPacket (dest);
  91. }
  92.